﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using POSSystem.BLL;
using MyGeneration.dOOdads;

namespace WebUI.EditPages
{
    public partial class EditRepairModel : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            base.Page_Load(sender, e);
            if (!IsPostBack)
            {
                RepairModelsKey = Guid.NewGuid().ToString();
                Utilities.LoadDropDownList(ddlBrand, Brands.LoadAllActiveBrands().DefaultView, Brands.ColumnNames.BrandName, Brands.ColumnNames.ID);
            }
            ((Popup)Master).ShowMessage("Ready");

            btnAdd.Enabled = SessionUser.BranchID == Branches.CurrentBranchID && SessionUser.Admin;
            btnEdit.Enabled = SessionUser.BranchID == Branches.CurrentBranchID && SessionUser.Admin;
            btnDelete.Enabled = SessionUser.BranchID == Branches.CurrentBranchID && SessionUser.Admin;
            btnEditProblem.Enabled = SessionUser.BranchID == Branches.CurrentBranchID && SessionUser.Admin;

            if (Branches.CurrentBranchID != SessionUser.BranchID || !SessionUser.Admin)
            {
                btnAdd.OnClientClick = "";
                btnDelete.OnClientClick = "";
                btnEdit.OnClientClick = "";
                btnEditProblem.OnClientClick = "";
            }
        }

        public string GetEditModelNamePageLink()
        {
            return this.ResolveClientUrl("~/EditPages/EditModelName.aspx?key=" + RepairModelsKey);
        }

        protected void ddlBrand_OnSelectedIndexChanged(object sender, EventArgs e)
        {

            if (ddlBrand.SelectedIndex > 0)
            {
                ReloadList();
            }
            else
            {
                lstModels.DataSource = null;
                lstModels.DataBind();
            }
        }

        public string RepairModelsKey
        {
            get 
            {
                return ViewState["RepairModelsKey"].ToString();
            }
            set
            {
                ViewState["RepairModelsKey"] = value;
            }
        }

        public RepairModels models
        {
            get { return Session[RepairModelsKey] == null ? null : (RepairModels)Session[RepairModelsKey]; }
            set { Session[RepairModelsKey] = value; }
        }

        private void ReloadList()
        {
            models = RepairModels.GetModelsByBrandID(int.Parse(ddlBrand.SelectedValue));
            Utilities.LoadListbox(lstModels, models.DefaultView, RepairModels.ColumnNames.ModelName, RepairModels.ColumnNames.ID);
        }

        private void RefreshList()
        {
            models.Filter = RepairModels.ColumnNames.Deleted + " = 0";
            Utilities.LoadListbox(lstModels, models.DefaultView, RepairModels.ColumnNames.ModelName, RepairModels.ColumnNames.ID);
        }

        protected void btnDelete_OnClick(object sender, EventArgs e)
        {
            try
            {
                if (ddlBrand.SelectedIndex > 0 && models != null)
                {
                    models = RepairModels.GetModelsByBrandID(int.Parse(ddlBrand.SelectedValue));

                    for (int i = lstModels.Items.Count - 1; i >= 0; i--)
                    {
                        if (lstModels.Items[i].Selected)
                        {
                            models.Filter = RepairModels.ColumnNames.ID + "=" + lstModels.Items[i].Value;
                            if (models.RowCount > 0)
                            {
                                models.Deleted = true;
                                models.LastModifiedByUser = SessionUser.ID;
                                models.LastModifiedDate = DateTime.UtcNow.AddHours(SysVars.TimeZone);
                            }
                        }
                    }

                    models.Save();
                    RefreshList();
                }
            }
            catch (Exception ex)
            {
                ErrorLog.WriteToFile("ProblemList.cs", "btnDelete_OnClick", ex.StackTrace);
                ((Popup)Master).ShowErrorMessage("Error:" + ex.Message);
            }
        }

        public void btnCancelProblem_OnClick(object sender, EventArgs e)
        {
            btnUpdateProblem.Visible = false;
            btnCancelProblem.Visible = false;

            btnEditProblem.Visible = true;
            lstProblems.ReadOnly = true;
            lstProblems.Refresh();
        }

        public void btnEditProblem_OnClick(object sender, EventArgs e)
        {
            btnUpdateProblem.Visible = true;
            btnCancelProblem.Visible = true;
            btnEditProblem.Visible = false;
            lstProblems.ReadOnly = false;
            lstProblems.Refresh();
        }

        public void btnUpdateProblem_OnClick(object sender, EventArgs e)
        {
            try
            {
                lstProblems.UpdateAll();
                ((Popup)Master).ShowMessage("Update successfully!");
                ((Popup)Master).UpdateStatus();
            }
            catch (Exception ex)
            {
                ((Popup)Master).ShowErrorMessage("Error: " + ex.Message);
                ((Popup)Master).UpdateStatus();
            }
        }

        protected void btnLoadProblems_OnClick(object sender, EventArgs e)
        {
            if(hidSelectedModelID.Value == "")
                return;

            lstProblems.ReadOnly = true;
            RepairModelProblems probs = RepairModelProblems.GetProblemsByModelID(int.Parse(hidSelectedModelID.Value));
            lstProblems.SourceObject = probs;
            lstProblems.Refresh();
            lstProblems.Columns[0].Visible = false;
            

            btnUpdateProblem.Visible = false;
            btnCancelProblem.Visible = false;
            btnEditProblem.Visible = SessionUser.Admin;
        }

        protected void btnAdd_OnClick(object sender, EventArgs e)
        {
            
             RefreshList();
           
        }
    }
}